/**
 * Created by isoft-work 5 on 1/17/2015.
 */
$(document).ready(function(){
   bindEventReportProduct();
});

function bindEventReportProduct() {
   ISS.EVENT.bindEvent('#result_report', 'click', function () {
      getProductReport();
   });

   ISS.EVENT.bindEvent('#option_report', 'change', function () {
      var _option = ISS.DOM.getVal("#option_report");
      if(_option == CONSTANT.ReportProduct.RevenueMonth) {
         ISS.DOM.show('#time_report');
      }
      else {
         ISS.DOM.hide('#time_report');
      }
   });
}

function getProductReport() {
//   validate
   var _option = ISS.DOM.getVal("#option_report");
   var _date = ISS.DOM.getVal("#input_name_time");
   if(_option) {
      if(_option == CONSTANT.ReportProduct.Remain) {
         getRemainProductReport();
      }
      else if(_option == CONSTANT.ReportProduct.RevenueMonth) {
         if(_date) {
            getRevenueMonthReport(_date);
         }
         else {
            alert('Chưa chọn ngày');
         }
      }
      else if(_option == CONSTANT.ReportProduct.Sale) {
         getSaleReport();
      }
   }
   else {
      alert('Chưa chọn thống kê');
   }
}

function getRemainProductReport() {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getNumberRemainProduct/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapRemainProductToView(responseContent);
      }
   });
}

function getRevenueMonthReport(date) {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getNumberSellProductForMonth/' + date,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapRevenueMonthToView(responseContent);
      }
   });
}

function getSaleReport() {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getNumberSaleProduct/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapSaleReportToView(responseContent);
      }
   });
}

function mapRemainProductToView(remainProduct) {
   if(remainProduct.length > 0) {
      var _html = '';
      for(var i = 0; i < remainProduct.length; i++) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductReport.remainList, remainProduct[i]);
      }
      ISS.DOM.html('#header_report_product', TEMPLATE.ProductReport.remainHeader);
      ISS.DOM.html('#list_report_product', _html);
      ISS.DOM.pagingAdmin('#result_product_report');
      ISS.DOM.show('#paging');
   }
}

function mapRevenueMonthToView(revenueMonth) {
   if(revenueMonth.length > 0) {
      var _html = '';
      for(var i = 0; i < revenueMonth.length; i++) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductReport.revenueList, revenueMonth[i]);
      }
      ISS.DOM.html('#header_report_product', TEMPLATE.ProductReport.revenueHeader);
      ISS.DOM.html('#list_report_product', _html);
      ISS.DOM.pagingAdmin('#result_product_report');
      ISS.DOM.show('#paging');
   }
}

function mapSaleReportToView(saleReport) {
   if(saleReport.length > 0) {
      var _html = '';
      for(var i = 0; i < saleReport.length; i++) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductReport.saleList, saleReport[i]);
      }
      ISS.DOM.html('#header_report_product', TEMPLATE.ProductReport.saleHeader);
      ISS.DOM.html('#list_report_product', _html);
      ISS.DOM.pagingAdmin('#result_product_report');
      ISS.DOM.show('#paging');
   }
}
